"""
File to house
bonuses both on a game-wide
and level-wide basis."""

import pygame
import data

import constants as c

def get_bonuses(gamestats, levelstats):
    achieved = []
    for bonus, kind in bonuses:
        if kind == 'game':
            stat = gamestats
        else:
            stat = levelstats
        b = bonus(stat)
        if b:
            achieved.append(b)
    return achieved

def transparent(f):
    def make_result_transparent(*args):
        surf = f(*args)
        trans = surf.get_at((0, 0))
        surf.set_colorkey(trans)
        return surf
    return make_result_transparent

def BadDriver(stats):
    'a bad driver bonus. killing all birds'
    if stats.saved > 0 or stats.dead == 0: return None

    text = 'Bad driver award\n   saved 0 birds.'
    return makeaward('bonus_baddriver.png', text)

def FriendOfMany(stats):
    'Never killed a single bird.'
    if stats.saved == 0 or stats.dead > 0: return None

    text = 'Friend of birds award\n   killed 0 birds.'
    return makeaward('bonus_friend.png', text)

def JnrPowerStation(stats):
    'Lifetime accumulation of much energy.'
    if stats.total_kWh < 10 or stats.total_kWh > 20:
        return None
    text = 'Jnr Power Station Lifetime award \n  accumulated more than 10 kWh'
    return makeaward('bonus_jnrpower.png', text)

def SnrPowerStation(stats):
    'Lifetime accumulation of much more energy.'
    if stats.total_kWh < 60:
        return None
    text = 'Snr Power Station Lifetime award\n  accumulated more than 60 kWh'
    return makeaward('bonus_snrpower.png', text)

def NetConsumer(stats):
    'user consumed more than produced for level.'
    if stats.total_kWh > 0:
        return None
    text = 'Net Consumer award\n   consumed %s kWh' % (-stats.total_kWh)
    return makeaward('bonus_netconsumer.png', text)

def MillionsofTransistors(stats):
    'a certain number of birds died in a level.'
    if stats.dead < 10:
        return None
    text = ('"Millions of tiny transistors" award\n'
            '   killed %s birds in a level') % stats.dead
    return makeaward('bonus_millions.png', text)


def Featherinyourcapaward(stats):
    'Lifetime saving of many birds.'
    if stats.saved < 20:
        return None
    text = 'featherinyourcap Lifetime award\n   Saved %s birds!' % stats.saved
    return makeaward('bonus_featherincap.png', text)

def GoldStar(stats):
    'Gold star for trying.'
    if stats.attempts < 5:
        return None
    text = 'Gold Star award\n   Tried and failed %s times' % stats.attempts
    return makeaward('bonus_goldstar.png', text)

@transparent
def makeaward(filename, txt):
    img, rect = data.load_image(filename, -1)

    surf = pygame.Surface((rect.width + 300, rect.height))
    surf.fill((150, 150, 150))
    surf.blit(img, (0, 0))
    normaltxt = pygame.font.Font(None, 24)

    # now draw some lines.
    points = ((0, surf.get_height()), surf.get_size(), (surf.get_width(), 0))
    surf.set_alpha(200)

    rendertxt(surf, txt, normaltxt, rect.width + 1, 10)

    return surf

def rendertxt(display, text, font, x, y):
    BIG_BUFFER = 30
    SMALL_BUFFER = 5
    lines = data.wrap_multi_line(text, font, 300)
    for line in lines:
        surf = font.render(line, True, c.WHITE)
        display.blit(surf, (x, y))
        y += SMALL_BUFFER + surf.get_height()
    return y + BIG_BUFFER


bonuses = [(BadDriver, 'level'),
           (FriendOfMany, 'level'),
           (JnrPowerStation, 'game'),
           (SnrPowerStation, 'game'),
           (NetConsumer, 'level'),
           (Featherinyourcapaward, 'game'),
           (GoldStar, 'level'),
           (MillionsofTransistors, 'level'),
          ]
